package beans;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.NamingException;

import tools.DBManager;

public class ComponentBean {
	
	private int id = 0;
	private String name = null;
	private int quantity = 0;
	private String comments= null;
	
	
	private Connection conn = null;
	private Statement stmt = null;
	private ResultSet rs = null;
	
	public ComponentBean(){};
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getQuantity() {
		return quantity;
	}
	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}
	public String getComments() {
		return comments;
	}
	public void setComments(String comments) {
		this.comments = comments;
	}
	
	public boolean setComponentByID(){
		if(id == 0)
			return false;
		try{
			conn = DBManager.getConnection();
			stmt = DBManager.getStatement(conn);
			String sqlCmd = String.format("SELECT * FROM t_component WHERE t_component.ID = %d ", id);
			rs = stmt.executeQuery(sqlCmd);
			
			rs.last();
			if(rs.getRow() != 1){
				return false;
			} else {
				setName(rs.getString("Name"));
				setQuantity(rs.getInt("Quantity"));
				setComments(rs.getString("Comments"));
			}
			
		} catch (SQLException sqlException){
			
		} catch (NamingException namingException){
			
		} finally{
			DBManager.close(rs);
			DBManager.close(stmt);
			DBManager.close(conn);
		}
		return true;
	}
	
	public boolean updateComponentByID(){
		if(id == 0)
			return false;
		try{
			conn = DBManager.getConnection();
			stmt = DBManager.getStatement(conn);
			String sqlCmd = String.format("SELECT * FROM t_component WHERE t_component.id = %d ", id);
			rs = stmt.executeQuery(sqlCmd);
			
			rs.last();
			if(rs.getRow() != 1){
				return false;
			} else {					
				rs.updateInt("Quantity", quantity);	
				rs.updateRow();
			}
			
		} catch (SQLException sqlException){
			
		} catch (NamingException namingException){
			
		} finally{
			DBManager.close(rs);
			DBManager.close(stmt);
			DBManager.close(conn);
		}
		return true;
	}
	
	public boolean insertComponent(){
		try{
			conn = DBManager.getConnection();
			stmt = DBManager.getStatement(conn);
			String sqlCmd = String.format("SELECT * FROM t_component");
			rs = stmt.executeQuery(sqlCmd);
			
			rs.moveToInsertRow();
			
			rs.updateString("Name",name);				
			rs.updateInt("Quantity", quantity);
		    rs.updateString("Comments",comments);
		    
			rs.insertRow();
			
			
		} catch (SQLException sqlException){
			
		} catch (NamingException namingException){
			
		} finally{
			DBManager.close(rs);
			DBManager.close(stmt);
			DBManager.close(conn);
		}
		return true;
	}
}



